package com.infore.dataReceiving.conf;
import org.quartz.JobDetail;
import org.quartz.Trigger;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.expression.ParseException;
import org.springframework.scheduling.quartz.CronTriggerFactoryBean;
import org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;
import com.infore.dataReceiving.quartz.Water2018ScheduledTasks;

@Configuration
public class Water2018QuartzConfig {
	
	// 配置中设定了
    // ① targetMethod: 指定需要定时执行scheduleInfoAction中的simpleJobTest()方法
    // ② concurrent：对于相同的JobDetail，当指定多个Trigger时, 很可能第一个job完成之前，
    // 第二个job就开始了。指定concurrent设为false，多个job不会并发运行，第二个job将不会在第一个job完成之前开始。
    // ③ cronExpression：0/10 * * * * ?表示每10秒执行一次，具体可参考附表。
    // ④ triggers：通过再添加其他的ref元素可在list中放置多个触发器。 scheduleInfoAction中的simpleJobTest()方法
	
    @Bean(name = "GetWaterRealTimeDataJobDetail")
    public MethodInvokingJobDetailFactoryBean GetWaterRealTimeDataJobDetail(Water2018ScheduledTasks scheduledTasks){
        MethodInvokingJobDetailFactoryBean bean = new MethodInvokingJobDetailFactoryBean ();
        bean.setTargetObject (scheduledTasks);
        bean.setTargetMethod ("GetWaterRealTimeData");
        bean.setConcurrent (false);
        return bean;
    }

    @Bean(name = "GetWaterRealTimeDataTrigger")
    public CronTriggerFactoryBean GetWaterRealTimeDataTrigger(JobDetail GetWaterRealTimeDataJobDetail){
        CronTriggerFactoryBean tigger = new CronTriggerFactoryBean ();
        tigger.setJobDetail (GetWaterRealTimeDataJobDetail);
        try {
            tigger.setCronExpression ("0 0/2 * * * ? ");//每2分钟执行一次
        } catch (ParseException e) {
            e.printStackTrace ();
        }
        return tigger;

    }
    
    
    @Bean(name = "GetWaterHourDataJobDetail")
    public MethodInvokingJobDetailFactoryBean GetWaterHourDataJobDetail(Water2018ScheduledTasks scheduledTasks){
        MethodInvokingJobDetailFactoryBean bean = new MethodInvokingJobDetailFactoryBean ();
        bean.setTargetObject (scheduledTasks);
        bean.setTargetMethod ("GetWaterHourData");
        bean.setConcurrent (false);
        return bean;
    }

    @Bean(name = "GetWaterHourDataTrigger")
    public CronTriggerFactoryBean GetWaterHourDataTrigger(JobDetail GetWaterHourDataJobDetail){
        CronTriggerFactoryBean tigger = new CronTriggerFactoryBean ();
        tigger.setJobDetail (GetWaterHourDataJobDetail);
        try {
            tigger.setCronExpression ("0 0/2 * * * ? ");//每2分钟执行一次
        } catch (ParseException e) {
            e.printStackTrace ();
        }
        return tigger;

    }
    
    
    @Bean(name = "GetWaterQualityControlDataJobDetail")
    public MethodInvokingJobDetailFactoryBean GetWaterQualityControlDataJobDetail(Water2018ScheduledTasks scheduledTasks){
        MethodInvokingJobDetailFactoryBean bean = new MethodInvokingJobDetailFactoryBean ();
        bean.setTargetObject (scheduledTasks);
        bean.setTargetMethod ("GetWaterQualityControlData");
        bean.setConcurrent (false);
        return bean;
    }

    @Bean(name = "GetWaterQualityControlDataTrigger")
    public CronTriggerFactoryBean GetWaterQualityControlDataTrigger(JobDetail GetWaterQualityControlDataJobDetail){
        CronTriggerFactoryBean tigger = new CronTriggerFactoryBean ();
        tigger.setJobDetail (GetWaterQualityControlDataJobDetail);
        try {
            tigger.setCronExpression ("0 0/2 * * * ? ");//每2分钟执行一次
        } catch (ParseException e) {
            e.printStackTrace ();
        }
        return tigger;
    }
    
    @Bean(name = "GetWaterEquipmentInfoJobDetail")
    public MethodInvokingJobDetailFactoryBean GetWaterEquipmentInfoJobDetail(Water2018ScheduledTasks scheduledTasks){
        MethodInvokingJobDetailFactoryBean bean = new MethodInvokingJobDetailFactoryBean ();
        bean.setTargetObject (scheduledTasks);
        bean.setTargetMethod ("GetWaterEquipmentInfo");
        bean.setConcurrent (false);
        return bean;
    }

    @Bean(name = "GetWaterEquipmentInfoTrigger")
    public CronTriggerFactoryBean GetWaterEquipmentInfoTrigger(JobDetail GetWaterEquipmentInfoJobDetail){
        CronTriggerFactoryBean tigger = new CronTriggerFactoryBean ();
        tigger.setJobDetail (GetWaterEquipmentInfoJobDetail);
        try {
            tigger.setCronExpression ("0 0/2 * * * ? ");//每2分钟执行一次
        } catch (ParseException e) {
            e.printStackTrace ();
        }
        return tigger;
    }
    
    @Bean
    public SchedulerFactoryBean schedulerFactory(Trigger GetWaterRealTimeDataTrigger,Trigger GetWaterHourDataTrigger,
    		Trigger GetWaterQualityControlDataTrigger,Trigger GetWaterEquipmentInfoTrigger){
        SchedulerFactoryBean bean = new SchedulerFactoryBean ();
        bean.setTriggers (GetWaterRealTimeDataTrigger,GetWaterHourDataTrigger,
        		GetWaterQualityControlDataTrigger,GetWaterEquipmentInfoTrigger);
        return bean;
    }


}
